{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Power Analysis for Password Bypass - SIMULATED Setup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**THIS IS NOT THE COMPLETE TUTORIAL - see file with `(MAIN)` in the name.**\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sure you don't have hardware, but that doesn't mean we can't have fun! If you check the ChipWhisperer based lab (using hardware), you'll find that the capture function is defined like this:\n",
    "    \n",
    "    def cap_pass_trace(pass_guess):   \n",
    "        ret = \"\"\n",
    "        reset_target(scope)\n",
    "        num_char = target.in_waiting()\n",
    "        while num_char > 0:\n",
    "            ret += target.read(num_char, 10)\n",
    "            time.sleep(0.01)\n",
    "            num_char = target.in_waiting()\n",
    "\n",
    "        scope.arm()\n",
    "        target.write(pass_guess)\n",
    "        ret = scope.capture()\n",
    "        if ret:\n",
    "            print('Timeout happened during acquisition')\n",
    "\n",
    "        trace = scope.get_last_trace()\n",
    "        return trace\n",
    "        \n",
    "This sends a password guess to the target device, and returns a power trace associated with the guess in question. So for example you could run:\n",
    "\n",
    "    cap_pass_trace(\"abcde\\n\")\n",
    "    \n",
    "To get a power trace of `abcde`.\n",
    "\n",
    "Instead, we have a function that uses pre-recorded data. Run the following block and it should give you access to a function that uses pre-recorded data. While how you use the function is the same, note the following limitations:\n",
    "\n",
    "* Not every combination is stored in the system -- instead it stores similar power traces.\n",
    "* 100 traces are stored for each guess, and it randomly returns one to still give you the effect of noise.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from cwtraces import sca101_lab_data\n",
    "import chipwhisperer as cw\n",
    "cap_pass_trace = sca101_lab_data[\"lab2_1\"][\"cap_pass_trace\"]\n",
    "\n",
    "trace_test = cap_pass_trace(\"h\\n\")\n",
    "\n",
    "#Basic sanity check\n",
    "assert(len(trace_test) == 3000)\n",
    "print(\"✔️ OK to continue!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But wait - this lab isn't the one you need to run it in! Instead copy the above block into the lab in the requested section, and you should be ready to rock."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
